import {createRouter, createWebHistory} from 'vue-router'

const routes = [
    {
        path: '/',
        name: 'layout',
        component: ()=> import("@/layout/layout"),
        redirect: "home",//重定项路由，当访问当前路由时，自动跳转到指定路由
        children: [     //嵌套路由
            {
                path: '/home',
                name: 'home',
                component: () => import("@/views/Home.vue")//另外一种加载组件方式
            },
            {
                path: "/book",
                name: 'book',
                component: () => import("@/views/book")
            }
        ]
    },
    {
        path: "/login",
        name: "login",
        component: () => import("@/views/login.vue")
    }, {
        path: "/register",
        name: "register",
        component: () => import("@/views/register")

    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})
/*router.beforeEach((to, from, next) => {

    //防止路由死循环空白页，需要进行判断
    //使用token，来进行权限验证，注册登录使用 window.localStorage.removeItem('token')
    if ( localStorage.getItem('token')||to.name === 'login' ){
        next()
    } else {
        next ({ name : 'login' });
    }


})*/

export default router
